/*
#======================================
# JavaScript registration functions
#===================================
# By David Sims
#======================================
*/

// Got to love prototype!
function authUsername()
{
	var usrname = document.forms['reg'].elements['username'].value;
	var url = './register?app=authusr&usrname=' + encodeURIComponent(usrname);

	new Ajax.Request(url, {
		method: 'get',
		onSuccess: function(http) {
			if ( parseInt(http.responseText) == 1 ) {
				$('username').update('<img src="./images/error.png" alt="" /> Username taken');
				document.forms['reg'].disabled = true;
				$('register').disabled = true;
			} else {
				$('username').update('<img src="./images/ok.png" alt="" />');
				document.forms['reg'].disabled = false;
				$('register').disabled = false;
			}
		}
	});
}

function authPassword()
{
	var password = document.forms['reg'].elements['password'];
	var confpass = document.forms['reg'].elements['confpass'];
	
	if ( password.value != confpass.value ) {
		$('password').update('<img src="./images/error.png" alt="" /> Invalid password');
		$('confpass').update('<img src="./images/error.png" alt="" /> Invalid password');
	} else {
		$('password').update('<img src="./images/ok.png" alt="" />');
		$('confpass').update('<img src="./images/ok.png" alt="" />');
	}
}

function authEmail()
{
	var em = document.forms['reg'].elements['email'];
	var ce = document.forms['reg'].elements['confemail'];
	if ( (!em.value.match(/^([a-z0-9\.\-_]+)@([a-z0-9\.\-]+)\.([a-z]{2,5})$/i)) || (em.value != ce.value) )
	{
		$('email').update('<img src="./images/error.png" alt="" /> Invalid email');
		$('confemail').update('<img src="./images/error.png" alt="" /> Invalid email');
	} else {
		$('email').update('<img src="./images/ok.png" alt="" />');
		$('confemail').update('<img src="./images/ok.png" alt="" />');
	}
}